﻿var logonDialog;
function checkAuthentication() {
    if (isAuthenticated) {
        userAuthenticated();
    }
    else {
        logonDialog = $('<div/>')[0];
        $(logonDialog).load('/Account/LogonOrRegister', '', function() {
            $(logonDialog).dialog({
                modal: true,
                resizable: false,
                title: 'Login',
                close: function() {
                    $(this).dialog('destroy');
                    $(this).remove();
                }
            });
        });
    }
}

//-------------FlaggQuestions-----------------------------
function FlagQuestion(qID) {
    if (isAuthenticated) {
        $.post("/Questions/QuestionToogleFlagged",
                { QuestionID: qID },
                OnFlagQuestionComplete,
                "json");
    }
    else {
        $("#favourite-question").feedback("Трябва да <a href='/Account/Logon/'>влезете</a> за да може да маркирате въпрос като неподходящ", { duration: 2000, right: true, type: "info" });
    }
}

function OnFlagQuestionComplete(data) {
    if (data.Success) {
        if (data.IsFlagged) {
            $("#FlaggQuestion").attr('class', 'flagg-post-icon flagg-post-yes');
        }
        else {
            $("#FlaggQuestion").attr('class', 'flagg-post-icon flagg-post-no');
        }
    }
    else {
        $("#FlaggQuestion").feedback(data.Message, { duration: 2000, right: true, type: "error" });
    }
}

//-------------Favourites-----------------------------
function ToogleFavourite(qID) {
    if (isAuthenticated) {
        $.post("/Questions/QuestionToogleFavourite",
                { QuestionID: qID },
                OnFavouriteComplete,
                "json");
    }
    else {
        $("#favourite-question").feedback("Трябва да <a href='/Account/Logon/'>влезете</a> за да може да добавите въпрос в своите любими", { duration: 2000, right: true, type: "info" });
    }
}

function OnFavouriteComplete(data) {
    if (data.Success) {
        if (data.IsFavourite) {
            $("#favourite-question").attr('class', 'favourite-question-icon favourite-question-yes');
        }
        else {
            $("#favourite-question").attr('class', 'favourite-question-icon favourite-question-no');
        }
    }
    else {
        $("#question_rating").feedback(data.Message, { duration: 2000, right: true, type: "error" });
    }
}

//---------------Question Votes--------------------------------------
function VoteForQuestion(qID, value) {
    if (isAuthenticated) {
        $.post("/QuestionVotes/Vote",
                { QuestionID: qID, VoteValue: value },
                OnVoteForQuestionComplete,
                "json");
    }
    else {
        $("#question_rating").feedback("Трябва да <a href='/Account/Logon/'>влезете</a> за да може да гласувате", { duration: 2000, right: true, type: "info" });
    }
}

function OnVoteForQuestionComplete(data) {
    if (data.Success) {
        if (data.AlreadyVoted) {
            $("#question_rating").feedback(data.Message, { duration: 2000, right: true, type: "error" });
        }
        else {
            var rating = parseInt($("#question_rating").html(), 0);
            $("#question_rating").html(rating + data.VoteValue);
        }

        if (data.VoteValue == 1) {
            $("#question_vote_up").attr('class', 'vote-icon voted-up');
            $("#question_vote_down").attr('class', 'vote-icon vote-down');
        }
        else if (data.VoteValue == -1) {
            $("#question_vote_up").attr('class', 'vote-icon vote-up');
            $("#question_vote_down").attr('class', 'vote-icon voted-down');
        }
    }
    else {
        $("#question_rating").feedback(data.Message, { duration: 2000, right: true, type: "error" });
    }
}


//---------------Answer Votes--------------------------------------
function VoteForAnswer(aID, value) {
    if (isAuthenticated) {
        $.post("/AnswerVotes/Vote",
                { AnswerID: aID, VoteValue: value },
                OnVoteForAnswerComplete,
                "json");
    }
    else {
        $("#answer_rating_" + aID).feedback("Трябва да <a href='/Account/Logon/'>влезете</a> за да може да гласувате", { duration: 2000, right: true, type: "info" });
    }
}

function OnVoteForAnswerComplete(data) {
    if (data.Success) {
        if (data.AlreadyVoted) {
            $("#answer_rating_" + data.AnswerID).feedback(data.Message, { duration: 2000, right: true, type: "error" });
        }
        else {
            var rating = parseInt($("#answer_rating_" + data.AnswerID).html(), 0);
            $("#answer_rating_" + data.AnswerID).html(rating + data.VoteValue);
        }

        if (data.VoteValue == 1) {
            $("#answer_vote_up_" + data.AnswerID).attr('class', 'vote-icon voted-up');
            $("#answer_vote_down_" + data.AnswerID).attr('class', 'vote-icon vote-down');
        }
        else if (data.VoteValue == -1) {
            $("#answer_vote_up_" + data.AnswerID).attr('class', 'vote-icon vote-up');
            $("#answer_vote_down_" + data.AnswerID).attr('class', 'vote-icon voted-down');
        }
    }
    else {
        $("#answer_rating_" + data.AnswerID).feedback(data.Message, { duration: 2000, right: true, type: "error" });
    }
}